package com.xingkeduo.dto.transter.carddetail;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.utils.DateUtils;
import com.xingkeduo.utils.WeekUtil;

import lombok.Data;

@Data
public class CardDetail {
	private String use_store;
	private String use_date;
	private String start_time;
	private String end_time;
	private String name;
	private String end_date;
	private String f_least_cost;
	private String f_pic;
	private String f_marketing_id;
    private String f_marketing_type;
    private String f_refund_type;
    private String f_servprod_names; 
    private String use_memo;
    private String receive_no;
    private String receive_time;
    private String receive_type;
    private String auth_status;
    private String gift_remark;
    private Boolean need_check;
    private String cert_status;
    private String f_store_ids;
	    
	public void covert(JSONObject json) {
		//this.use_store = json.getOrDefault("storeNames", "")==null?"":json.getOrDefault("storeNames", "").toString();
		
		this.f_marketing_id = json.getOrDefault("marketingId", "")==null?"":json.getOrDefault("marketingId", "").toString();
		this.f_marketing_type = json.getOrDefault("marketingType", "")==null?"":json.getOrDefault("marketingType", "").toString();
		this.gift_remark = json.getOrDefault("giftRemark", "")==null?"":json.getOrDefault("giftRemark", "").toString();
		this.receive_no = json.getOrDefault("receiveNo", "")==null?"":json.getOrDefault("receiveNo", "").toString();
		this.auth_status = json.getOrDefault("authStatus", "0")==null?"0":json.getOrDefault("authStatus", "0").toString();
		String cardType = json.getOrDefault("serviceType", "") ==null?"": json.getOrDefault("serviceType", "").toString();
		this.need_check = Boolean.FALSE;
		this.cert_status = auth_status;
		if (StringUtils.isNotBlank(auth_status)) {
			if ("FREE".equalsIgnoreCase(cardType)) {
				this.need_check = Boolean.TRUE;
			} else if("DISCOUNT".equalsIgnoreCase(cardType) &&auth_status.equals("0")){
				this.need_check = Boolean.TRUE;
			}else {
				this.need_check = Boolean.FALSE;
			}
			if ("0".equals(auth_status)) {
				this.cert_status = "0";
			} else if ("1".equals(auth_status)) {
				this.cert_status = "1";
			} else if ("2".equals(auth_status)) {
				this.cert_status = "-1";
			} else if ("3".equals(auth_status)) {
				this.cert_status = "2";
			}
		}
		this.receive_time = DateUtils.formatDate(json.getDate("createTime"), DateUtils.DEFAULT_DATE_TIME_FORMAT);
		this.receive_type = json.getOrDefault("sendType", "")==null?"":json.getOrDefault("sendType", "").toString();
		JSONObject marketing = json.getJSONObject("marketingInfo");
		this.f_refund_type = marketing.getOrDefault("refundType", "")==null?"":marketing.getOrDefault("refundType", "").toString();
		this.f_servprod_names = marketing.getOrDefault("supportServprodNames", "")==null?"":marketing.getOrDefault("supportServprodNames", "").toString();
		String works = marketing.getOrDefault("weekHourIds", "")==null?"":marketing.getOrDefault("weekHourIds", "").toString();
		this.use_memo=marketing.getOrDefault("useMomo", "")==null?"":marketing.getOrDefault("useMomo", "").toString();
		if (StringUtils.isBlank(works) || "0".equals(works)) {
			this.use_date = "周一至周日 0-24点";
		} else {
			JSONArray array = marketing.getJSONArray("activityDays");
			if (array != null && array.size() > 0) {
				StringBuffer daysInfo = new StringBuffer();
				int size = array.size();
				for(int i=0;i<size;i++) {
					JSONObject o = array.getJSONObject(i);
					String daysIds = o.getOrDefault("dayids", "")==null?"":o.getOrDefault("dayids", "").toString();
					if (StringUtils.isBlank(daysIds)) {
						continue;
					}
					String startTime = o.getOrDefault("startTime", "").toString();
					int start = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(startTime)));
					String endTime = o.getOrDefault("endTime", "").toString();
					int end = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(endTime)));
					if (daysIds.startsWith(",")) {
						daysIds = daysIds.substring(1, daysIds.length());
					}
					if (daysIds.endsWith(",")) {
						daysIds = daysIds.substring(0, daysIds.length() - 1);
					}
					daysInfo.append(WeekUtil.convertWeek(daysIds)).append(" ")
							.append(start).append("-")
							.append(end).append("点; ");
				}
				if(StringUtils.isBlank(daysInfo.toString())) {
					this.use_date ="周一至周日 0-24点";
				}else {
					this.use_date =daysInfo.toString();
				}
			}else {
				this.use_date ="周一至周日 0-24点";
			}
		}
		Date startDate = json.getDate("useStartTime");
		if (startDate != null) {
			this.start_time = DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT_V3);
		}
		Date endDate = json.getDate("useEndTime");
		if (endDate != null) {
			this.end_time = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);
		}else if(json.getOrDefault("usePeriod", "0")!=null){
			Integer usePeriod = Integer.parseInt(json.getOrDefault("usePeriod", "0").toString());
			this.end_date = DateUtils.formatDate(DateUtils.addSomeDays(startDate, usePeriod), DateUtils.DATE_FORMAT_V3);
		}
		this.name = json.getOrDefault("marketingName", "")==null?"":json.getOrDefault("marketingName", "").toString();
		this.end_date = start_time + "-" + end_time;
	    BigDecimal leastAmount = new BigDecimal(json.getOrDefault("leastCost", "0")==null?"0":json.getOrDefault("leastCost", "0").toString());
		leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
		this.f_least_cost = leastAmount.toString();
	    this.f_pic = "common";
	    String storeIds = marketing.getOrDefault("storeIds", "0")==null?"0": marketing.getOrDefault("storeIds", "0").toString();
		if(StringUtils.isNotBlank(storeIds)) {
			if(storeIds.startsWith("0,")) {
				storeIds = storeIds.substring(2, storeIds.length());
			}
			if(storeIds.endsWith(",0")) {
				storeIds = storeIds.substring(0, storeIds.length()-2);
			}
		}
		this.f_store_ids = storeIds;
		this.use_store="适用部分门店";
		if("0".equals(storeIds)) {
			this.use_store="适用所有门店";
		}
	}
}
